package com.xiaotu.storage.converter;

import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import com.xiaotu.common.exception.SpiderException;
import com.xiaotu.common.exception.SpiderExceptionCode;
import com.xiaotu.common.util.DateUtils;
import com.xiaotu.common.util.RegexUtils;

import net.sf.json.JSONObject;

/**
 * @类名 DateConverter
 * @日期 2017年2月22日
 * @作者 高海军
 * @功能
 */
@Component("DateConverter")
public class DateConverter extends AConverter
{
	public static final String DATE_FORMATE = "yyyy-MM-dd HH:mm:ss";
	
	@Override
	protected Object doConvert(String value, JSONObject target,
			boolean needThrow)
	{
		String date = RegexUtils.regexSearch(
				"(\\d{4})-(\\d{2})-(\\d{2}) (\\d{2}):(\\d{2}):(\\d{2})", value);
		if (StringUtils.isNotEmpty(date))
			return date;
		date = RegexUtils.regexSearch("(\\d{4})-(\\d{2})-(\\d{2})", value);
		if (StringUtils.isNotEmpty(date))
			return date + " 00:00:00";
		date = RegexUtils.regexSearch("(\\d{13})", value);
		if (StringUtils.isNotEmpty(date))
			return DateUtils.fomartDateToString(new Date(Long.parseLong(date)),
					DATE_FORMATE);
		date = RegexUtils.regexSearch("(\\d{10})", value);
		if (StringUtils.isNotEmpty(date))
			return DateUtils.fomartDateToString(
					new Date(Long.parseLong(date + "000")), DATE_FORMATE);
		
		date = RegexUtils.regexSearch("秒前|分钟前|小时前|昨天|前天|天前|周前|月前", value);
		if (StringUtils.isEmpty(date))
		{
			if (needThrow)
				throw new SpiderException(
						SpiderExceptionCode.DateErrorException,
						"target:" + target);
			return value;
		}
		
		String num = RegexUtils.regexSearch(RegexUtils.REGEX_INTEGER, value);
		if (StringUtils.isNotEmpty(
				RegexUtils.regexSearch("秒前|分钟前|小时前|天前|周前|月前", value))
				&& StringUtils.isEmpty(num))
		{
			if (needThrow)
				throw new SpiderException(
						SpiderExceptionCode.DateErrorException,
						"target:" + target);
			return value;
		}
		
		if (!target.has("timestamp")
				|| !(target.get("timestamp") instanceof Number))
		{
			if (needThrow)
				throw new SpiderException(
						SpiderExceptionCode.TimeStampErrorException,
						"target:" + target);
			return value;
		}
		
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(target.getLong("timestamp"));
		switch (date)
		{
			
			case "秒前":
				calendar.add(Calendar.SECOND, Integer.parseInt(num) * -1);
				break;
			case "分钟前":
				calendar.add(Calendar.MINUTE, Integer.parseInt(num) * -1);
				break;
			case "小时前":
				calendar.add(Calendar.HOUR, Integer.parseInt(num) * -1);
				break;
			case "昨天":
				calendar.add(Calendar.DAY_OF_YEAR, -1);
				break;
			case "前天":
				calendar.add(Calendar.DAY_OF_YEAR, -2);
				break;
			case "天前":
				calendar.add(Calendar.DAY_OF_YEAR, Integer.parseInt(num) * -1);
				break;
			case "周前":
				calendar.add(Calendar.WEEK_OF_YEAR, Integer.parseInt(num) * -1);
				break;
			case "月前":
				calendar.add(Calendar.MONTH, Integer.parseInt(num) * -1);
				break;
			default:
				break;
		}
		
		return DateUtils.fomartDateToString(calendar.getTime(), DATE_FORMATE);
	}
}
